<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button type="primary" class="filter-item" size="small" @click="handleCreate">新增</el-button>

      <el-input v-model="listQuery.key" placeholder="key" style="width: 200px;" class="filter-item" size="small" clearable/>
      <el-button type="primary" class="filter-item" size="small" @click="handleFilter">搜索</el-button>
    </div>

    <el-table
            v-loading="listLoading"
            :data="list"
            size="small"
            element-loading-text="Loading"
            border
            fit
            highlight-current-row>
      <el-table-column label="ID" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>

      <el-table-column label="key" min-width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.key }}
        </template>
      </el-table-column>

      <el-table-column label="value" min-width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.value }}
        </template>
      </el-table-column>

      <el-table-column label="备注" min-width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.remark }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleUpdate(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
              :current-page="listQuery.page"
              :page-sizes="[10, 20, 30, 50, 100]"
              :page-size="listQuery.limit"
              :total="total"
              background
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
      />
    </div>

    <!-- 详情 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="60%" :close-on-click-modal="false">
      <el-form ref="form" :rules="rules" :model="form" label-width="180px">
        <el-form-item label="key" prop="key">
          <el-input :disabled="dialogStatus==='update'" type="text" v-model="form.key" clearable />
        </el-form-item>

        <el-form-item label="value" prop="value">
          <el-input type="text" v-model="form.value" clearable />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input type="text" v-model="form.remark" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="saveData" :loading="btnLoading">确定</el-button>
      </div>
    </el-dialog>
    <!-- 详情 -->
  </div>
</template>

<script>
  import request from '@/utils/request'

  export default {
    data() {
      return {
        list: null,
        total: 0,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 10,
          key: '',
        },
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          create: '新增',
          update: '修改',
        },
        form: {
          id: '',
          key: '',
          value: '',
          remark: '',
        },
        rules: {
          key: [{ required: true, message: 'key不能为空', trigger: 'change' }],
          value: [{ required: true, message: 'value不能为空', trigger: 'change' }],
        },
        btnLoading: false,
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        request({
          url: '/api/backend/keyValue/list',
          method: 'get',
          params: this.listQuery,
        }).then(response => {
          this.list = response.data.items
          this.total = response.data.total
        }).finally(()=>{
          this.listLoading = false;
        })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      handleSizeChange(val) {
        this.listQuery.limit = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      resetForm() {
        this.form = {
          id: '',
          help_center_type_id: '',
          name: '',
          content: '',
        }
        this.btnLoading = false
      },
      handleCreate() {
        this.resetForm()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['form'].clearValidate() //清除校验结果
        })
      },
      handleUpdate(row) {
        this.resetForm()
        this.form = Object.assign({}, row)
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['form'].clearValidate() //清除校验结果
        })
      },
      saveData() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.btnLoading = true
            request({
              url: '/api/backend/keyValue/save',
              method: 'post',
              data: this.form
            }).then(() => {
              this.dialogFormVisible = false
              this.$message({
                type: 'success',
                message: '操作成功'
              });
              this.getList();
            }).catch((err) => {
              console.log(err)
              this.btnLoading = false
            }).finally(() => {
              this.btnLoading = false
            })
          }
        })
      },
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
